package org.quipu.client;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import org.quipu.client.events.AppEvent;
import org.quipu.client.events.AppEventBus;
import org.quipu.client.events.AppEventListener;
import org.quipu.client.events.ErrorEvent;

public class ErrorWidget extends AppEventWidget implements AppEventListener {

    private final VerticalPanel vPanel;

    public ErrorWidget(AppEventBus eventBus, AppCache cache) {
        super(eventBus, cache);
        vPanel = new VerticalPanel();
    }

    @Override
    public Widget getWidget() {
        return vPanel;
    }

    @Override
    public void init() {
        getEventBus().addListener(ErrorEvent.class, this);
    }

    @Override
    public void handleEvent(AppEvent event) {
        if(event != null && event instanceof ErrorEvent) {
            String message = ((ErrorEvent) event).getMessage();
            vPanel.insert(new Label(message), 0);
            while(vPanel.getWidgetCount() > 10) {
                vPanel.remove(10);
            }
        }
    }
}
